package com.ruoyi.controller;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.entity.UserWechat;
import com.ruoyi.common.entity.dto.WechatLoginDTO;
import com.ruoyi.common.service.UserWechatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 微信登录控制器
 * 处理微信小程序/公众号登录相关请求
 */
@RestController
@RequestMapping("/api/auth/wechat")
@Api(tags = "微信登录相关接口")
public class WechatLoginController {

    @Autowired
    private UserWechatService wechatLoginService;

    /**
     * 微信登录（小程序）
     * 通过code获取openid并完成登录流程
     */
    @PostMapping("/login")
    @Anonymous
    @ApiOperation(value = "微信小程序登录", notes = "使用微信返回的code进行登录")
    public AjaxResult login(@Valid @RequestBody WechatLoginDTO loginDTO) {
        // 调用服务层处理登录逻辑，返回包含token和用户信息的结果
        String token = wechatLoginService.login(loginDTO.getCode());
        return AjaxResult.success().put(Constants.TOKEN, token);
    }

    /**
     * 微信绑定手机号
     * 小程序中获取用户手机号并绑定
     *//*
    @PostMapping("/bind-phone")
    @ApiOperation(value = "绑定手机号", notes = "绑定微信用户手机号")
    public R bindPhone(@RequestParam String encryptedData,
                             @RequestParam String iv,
                             @RequestHeader String token) {
        wechatLoginService.bindPhone(encryptedData, iv, token);
        return null;
    }

    *//**
     * 获取当前登录用户信息
     *//*
    @GetMapping("/user-info")
    @ApiOperation(value = "获取用户信息", notes = "获取当前登录用户的详细信息")
    public R<UserWechat> getUserInfo(@RequestHeader String token) {
       UserWechat userWechat = wechatLoginService.getUserInfo(token);
        return R.ok(userWechat);
    }

    *//**
     * 微信公众号登录
     * 通过code获取用户信息并登录
     *//*
    @GetMapping("/mp/login")
    @ApiOperation(value = "微信公众号登录", notes = "微信公众号网页授权登录")
    public R mpLogin(@RequestParam String code,
                           @RequestParam(required = false) String redirectUri) {
        return wechatLoginService.mpLogin(code, redirectUri);
    }*/
}
    